﻿#ifndef TCPSOCKETNETWORKCENTER_H
#define TCPSOCKETNETWORKCENTER_H

#include <QMutex>
#include <QTcpServer>
#include <QTcpSocket>
#include <QThread>

class TcpSocketNetWorkCenter : public QThread
{
    Q_OBJECT
public:
    /**
    * 获取单例实例
    * @return 返回单例实例
    */
    static TcpSocketNetWorkCenter* getInstance();
    /**
    * 删除单例实例
    */
    static void deleteInstance();
    /**
     * @brief sendMessage 发送消息
     * @param channel
     * @param message
     */
    void sendMessage(QString &message);



private:
    explicit TcpSocketNetWorkCenter(QObject *parent = nullptr);
    ~TcpSocketNetWorkCenter();
     quint16 getSendPackId();
private:
    static TcpSocketNetWorkCenter* instance;
    static QMutex mutex;
signals:
    void signal_send_message(QString &message);
private slots:
    void slot_send_message(QString &message);
    void handleNewConnection();
    void onSocketDisconnected();
//    void processTextMessage(QString message);
private:
    QTcpServer *m_server;
    quint16 sendPackId;

    QMutex clientMapMutex;
    QMap<QString, QTcpSocket*> clientMap;
protected:
    void run() override;

};

#endif // TCPSOCKETNETWORKCENTER_H
